/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef LIB2D_GUI_EDITBOX_H
#define LIB2D_GUI_EDITBOX_H

#include "Object.h"
#include <xml/Object.h>
#include "view/EditBox.h"

namespace liba
{
namespace lib2d
{
namespace gui
{

class EditBox :
	public Object,
	public xml::ObjectANSI
{
public:
    EditBox( Object * parent );
    virtual ~EditBox();

	virtual view::EditBox * get_view()const
	{
		return view;
	}

	const std::wstring & get_text()const
	{
		return text;
	}

	virtual bool on_attribute( xml::Provider * prov, const std::string & name, const std::string & value );
	virtual bool on_node( xml::Provider * prov, const std::string & name );

	virtual void on_text( const std::wstring & text );
	virtual void on_key_down( const std::string & key_name );

	virtual void on_button_down( const Vector2 & point, const Atom & button_name );
	virtual void on_button_up( const Vector2 & point, const Atom & button_name );
	virtual void on_mouse_move( const Vector2 & point );
	virtual void on_mouse_move_outside( const Vector2 & point );

	virtual void on_focus( bool focus );

private:
	std::wstring text;
	unsigned cursor_pos;
	unsigned sel_end;

	AutoPtr< view::EditBox > view;
	friend class view::EditBox;

	std::pair< int, int > get_selection_range()const
	{
		if( cursor_pos > sel_end )
			return std::make_pair( sel_end, cursor_pos );

		return std::make_pair( cursor_pos, sel_end );
	}
};

} //namespace gui
} //namespace lib2d
} //namespace liba

using namespace liba;

#endif //LIB2D_GUI_EDITBOX_H
